package com.sjx.onelinecord.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.sjx.onelinecord.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class CustomDialog extends Dialog {

    public CustomDialog(@NonNull Context context) {
        super(context);
    }

    public CustomDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected CustomDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    //显示的图片
    private ImageView imageIv;

    //显示的标题
    private TextView titleTv;

    //显示的消息
    private TextView messageTv;

    //确认和取消按钮
    private Button negativeBtn, positiveBtn;

    //按钮之间的分割线
    private View columnLineView;

    //以下内容数据
    private String message;
    private String title;
    private String positive, negative;
    private int imageResId = -1;

    public OnClickBottomListener onClickBottomListener;

    //底部是否只有一个按钮
    private boolean isSingle = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_dialog);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);
        //初始化界面控件
        initView();
        //初始化界面数据
        refreshView();
        //初始化界面控件的事件
        initEvent();
    }

    //初始化界面的显示数据
    private void refreshView() {

        if (!TextUtils.isEmpty(title)) {
            titleTv.setText(title);
            titleTv.setVisibility(View.VISIBLE);
        } else {
            titleTv.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(message)) {
            messageTv.setText(message);
        }
        if (!TextUtils.isEmpty(positive)) {
            messageTv.setText("确定");
        }
        if (!TextUtils.isEmpty(negative)) {
            messageTv.setText("取消");
        }
        if (imageResId != -1) {
            imageIv.setImageResource(imageResId);
            imageIv.setVisibility(View.VISIBLE);
        } else {
            imageIv.setVisibility(View.GONE);
        }
        if (isSingle) {
            columnLineView.setVisibility(View.GONE);
            negativeBtn.setVisibility(View.GONE);
        } else {
            negativeBtn.setVisibility(View.VISIBLE);
            columnLineView.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void show() {
        super.show();
        refreshView();
    }

    private void initView() {
        negativeBtn = (Button) findViewById(R.id.negative);
        positiveBtn = (Button) findViewById(R.id.positive);
        titleTv = (TextView) findViewById(R.id.title);
        messageTv = (TextView) findViewById((R.id.message));
        imageIv = (ImageView) findViewById(R.id.image);
        columnLineView = findViewById(R.id.column_line);
    }

    //初始化界面的确定和取消监听器
    private void initEvent() {
        positiveBtn.setOnClickListener(v -> {
            if (onClickBottomListener != null) {
                onClickBottomListener.onPositiveClick();
            }
        });

        negativeBtn.setOnClickListener(v -> {
            if (onClickBottomListener != null) {
                onClickBottomListener.onNegativeClick();
            }
        });
    }

    public String getMessage() {
        return message;
    }

    public CustomDialog setMessage(String message) {
        this.message = message;
        return this;
    }

    public String getTitle() {
        return title;
    }

    public CustomDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public String getPositive() {
        return positive;
    }

    public CustomDialog setPositive(String positive) {
        this.positive = positive;
        return this;
    }

    public String getNegative() {
        return negative;
    }

    public CustomDialog setNegative(String negative) {
        this.negative = negative;
        return this;
    }

    public int getImageResId() {
        return imageResId;
    }

    public CustomDialog setImageResId(int imageResId) {
        this.imageResId = imageResId;
        return this;
    }

    public boolean isSingle() {
        return isSingle;
    }

    public CustomDialog setSingle(Boolean b) {
        this.isSingle = b;
        return this;
    }

    public CustomDialog setGravity(String position) {
        if (position == "top") {
            this.getWindow().setGravity(Gravity.TOP);
        } else if (position == "bottom") {
            this.getWindow().setGravity(Gravity.BOTTOM);
        } else {
            this.getWindow().setGravity(Gravity.CENTER);
        }
        return this;
    }

    public void setOnClickBottomListener(OnClickBottomListener listener) {
        this.onClickBottomListener = listener;
    }

    public interface OnClickBottomListener {
        void onPositiveClick();

        void onNegativeClick();
    }

}
